package ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ssm.pojo.UserSsm;
import ssm.service.UserService;
import ssm.util.JSONResult;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/addUser")
    public ModelAndView toAddUser() {
        ModelAndView modelAndView = new ModelAndView("addUser");
        return modelAndView;
    }

    @RequestMapping("/listUser")
    public ModelAndView toListUser() {
        ModelAndView modelAndView = new ModelAndView("listUser");
        return modelAndView;
    }

    @RequestMapping("/updateUser")
    public ModelAndView toUpdateUser() {
        ModelAndView modelAndView = new ModelAndView("updateUser");
        return modelAndView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult addUser(HttpServletRequest request) {
        JSONResult result;
        UserSsm userSsm = aa(request);
        System.out.println(userSsm);
        if (userService.addUser(userSsm)) {
            result = new JSONResult(userSsm);
            return result;
        } else {
            result = new JSONResult();
        }
        return result;
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult listUser() {
        System.out.println("list层");
        List<UserSsm> list = userService.findAll();
        System.out.println("list完成");
        return new JSONResult(list);
    }


    @RequestMapping(value = "/update", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult updateUser(HttpServletRequest request,String id) {
        System.out.println("执行修改controller");
        JSONResult result;
        Integer uid = Integer.parseInt(request.getParameter("id"));
        System.out.println("id="+uid);
        UserSsm userSsm = aa(request);
        userSsm.setId(uid);
        System.out.println(userSsm);
        if (userService.updateUser(userSsm)){
            result = new JSONResult(userSsm);
            System.out.println("修成功controller");
            return result;

        }else {
            result = new JSONResult();
            return result;
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult deleteUser(HttpServletRequest request) {
        JSONResult result = new JSONResult();
        Integer id =Integer.parseInt(request.getParameter("id"));
        if (!userService.deleteUserById(id)){
            result.setMessage("error");
        }
        return result;
    }

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult findUserById(HttpServletRequest request) {
        JSONResult result;
        Integer id =Integer.parseInt(request.getParameter("id"));
        UserSsm userSsm = userService.findUserById(id);
        result = new JSONResult(userSsm);
        result.setMessage("success");
        return result;
    }


    private UserSsm aa(HttpServletRequest request){
        UserSsm userSsm = new UserSsm();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        userSsm.setUsername(username);
        userSsm.setPassword(password);
        userSsm.setPhone(phone);
        return userSsm;
    }





}



























































